package com.sailing.lianxi.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 匿名类创建线程
 * @author Administrator
 *
 */
public class AnonyThread {

	public static void main(String[] args) {
		//方式1
		Thread t1 = new Thread() {
			public void run() {
				System.out.println("线程"+Thread.currentThread().getName()+" 运行");
			}
		};
		t1.start();
		
		//方式二
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("线程"+Thread.currentThread().getName()+" 运行");
			}
		});
		t2.start();
		
		//方式三
		FutureTask<String> ft = new FutureTask<>(new Callable<String>() {
			@Override
			public String call() throws Exception {
				// TODO Auto-generated method stub
				return "你好";
			}
		});
		Thread t3 = new Thread(ft);
		t3.start();
		String result;
		try {
			result = ft.get();
			System.out.println("获取线程+"+Thread.currentThread().getName()+"返回值："+result);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
